<!DOCTYPE html>
<html lang="zxx">

<head>
    <title>登录</title>
    <!-- Meta tag Keywords -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8"/>
    <meta name="keywords"
          content="Report Login Form Responsive web template, Bootstrap Web Templates, Flat Web Templates, Android Compatible web template, Smartphone Compatible web template, free webdesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design"/>
    <!-- //Meta tag Keywords -->
    <link href="../../static/plugins/bootstrap-5.3.7-dist/css/bootstrap.min.css">
    <link href="../../static/css/login/css2.css" rel="stylesheet">
    <!--/Style-CSS -->
    <link rel="stylesheet" href="../../static/css/login/style.css" type="text/css" media="all"/>
    <!--//Style-CSS -->

    <link rel="stylesheet" href="../../static/css/login/font-awesome.min.css" type="text/css" media="all">

</head>

<body>

<!-- form section start -->
<section class="w3l-hotair-form" style="background: #0568C1 ">
    <h1>欢迎回来</h1>
    <div class="container">
        <!-- /form -->
        <div class="workinghny-form-grid">
            <div class="main-hotair">
                <div class="content-wthree">
                    <h2>登录</h2>
                    <form method="post" id="loginForm">
                        <input type="text" class="text" name="username" placeholder="用户名" required="" autofocus>
                        <input type="password" class="password" name="password" placeholder="密码" required=""
                               autofocus>
                        <div class="form-control row">
                            <input type="text" class="text" name="vcode" placeholder="验证码" required="" autofocus
                                   style="width: 220px;">
                            <img src="/vcode" id="loginvcode"
                                 style="vertical-align: middle;margin-bottom: 10px;cursor: pointer"
                                 onclick="this.src='/vcode?'+Math.random()">
                        </div>

                        <button class="btn" type="submit" style="margin-top: 10px">登录</button>
                    </form>

                    <p class="account">还没有账户? <a href="/reg" style="margin-right: 15px">注册</a>
                        <a href="{{url_for('auth.forgot_pwd')}}" class="account">
                            忘记密码
                        </a></p>
                    <span style="color: red"></span>
                </div>
                <div class="w3l_form align-self">
                    <div class="left_grid_info">
                        <img src="../../static/img/login/1.png" class="img-fluid">
                    </div>
                </div>
            </div>
        </div>
        <!-- //form -->
    </div>
    <!-- copyright-->
    <div class="copyright text-center">
        <p class="copy-footer-29">© 2025. All rights reserved</p>
    </div>
    <!-- 从Flask后端传递的上一个页面URL -->
    <div id="prevPageUrl" style="display: none">{{ prev_page or '' }}</div>
    <!-- //copyright-->
</section>
<!-- //form section start -->

<script src="../../static/js/jquery-3.7.1.min.js"></script>
<script>
    document.getElementById('loginForm').addEventListener('submit', function (event) {
        event.preventDefault();// 阻止默认提交行为
        const form = this;
        const formData = new FormData(form);//收集表单数据
        fetch('/login', {method: 'POST', body: formData}).then(response => {
            if (!response.ok) {
                throw new Error('网络响应错误！');
            } else {
                return response.text();// 假设服务器返回 字符串 格式
            }
        }).then(data => {
            if (data == 'vcode-error') {
                alert('校验码错误');
            } else if (data == 'login-false') {
                alert('用户名不存在或密码错误！')
                window.location.reload();
            } else {
                alert(data.message || '登录成功！');
                // 获取上一个页面URL或默认首页
                var next_url = document.getElementById('prevPageUrl').textContent
                if (next_url){
                    window.location.href = next_url;
                    console.log(next_url)
                }else{
                    window.location.href='/';
                }

            }
        }).catch(error => {
            console.error("错误", error);
            alert('登录过程中发生错误，请稍后再试。')
        })
    })

</script>

</body>

</html>